library(tidyverse)
library(rio)
library(cregg)
library(patchwork)

# set replication folder as working directory
setwd("~replication")

load("data_genderedcost_long.rdata")

# only include completed answers - and answers given before deadline
# 2021-12-20 21:49:52 was the last response within the time frame
df_long <- df_long %>% 
  filter(SurveyStatus==2)

df_long <- df_long %>% 
  filter(SurveyEndTime<="2021-12-20 21:49:52")


## MARGINAL MEANS FOR GENDER/VICTIM

df_1 <- df_long %>% 
  dplyr::filter(woman=="Woman" & victim=="Victim")

df_2 <- df_long %>% 
  dplyr::filter(woman=="Woman" & victim=="Non-victim")

df_3 <- df_long %>% 
  dplyr::filter(woman=="Man" & victim=="Victim")

df_4 <- df_long %>% 
  dplyr::filter(woman=="Man" & victim=="Non-victim")


mm_1 <- cregg::mm(df_1, # data
                  choice~position + remuneration + workload + work_environment, # formula
                  id = id)

mm_2 <- cregg::mm(df_2, # data
                  choice~position + remuneration + workload + work_environment, # formula
                  id = id)

mm_3 <- cregg::mm(df_3, # data
                  choice~position + remuneration + workload + work_environment, # formula
                  id = id)

mm_4 <- cregg::mm(df_4, # data
                  choice~position + remuneration + workload + work_environment, # formula
                  id = id)

mm_1$type <- "Woman & Victim"
mm_2$type <- "Woman & Non-victim"
mm_3$type <- "Man & Victim"
mm_4$type <- "Man & Non-victim"

mm_gender_victim <- bind_rows(mm_1, mm_2, mm_3, mm_4) %>%
  mutate(feature = case_when(feature=="work_environment"~"Working Environment",
                             feature=="workload"~"Workload",
                             feature=="remuneration"~"Remuneration",
                             feature=="position"~"Position")) %>% 
  mutate(feature = factor(feature, levels = c("Position", "Remuneration", "Workload", "Working Environment"))) %>%
  mutate(type = factor(type, levels = c("Woman & Victim", "Man & Victim", "Woman & Non-victim", "Man & Non-victim")))

mm_gender_victim %>% 
  ggplot(data=., aes(y = level, x = estimate, color = type, shape = type, linetype = type)) +
  geom_point(position = position_dodge2(width = 0.8)) +
  geom_linerange(aes(xmin=estimate-(std.error*1.39),
                     xmax=estimate+(std.error*1.39)),
                 position = position_dodge2(width = 0.8)) +
  xlab("Marginal mean") +
  ylab("") +
  scale_color_manual("", values = c("gray", "black", "gray", "black")) +
  scale_shape_manual("", values=c(2,1,17,16)) + #(16,17,1,2)
  scale_linetype_manual("", values = c(2,2,1,1)) +
  #scale_color_grey("") +
  #scale_shape_discrete("") +
  scale_x_continuous(breaks = seq(0.2,0.8,0.05), labels = seq(0.2,0.8,0.05)) +
  geom_vline(xintercept = 0.5, linetype = "dashed") +
  theme_bw() +
  facet_wrap(~feature, scales = "free_y", ncol = 1) +
  theme(legend.position = "top",
        legend.justification='left',
        panel.background = element_rect(fill = "white"),
        strip.background = element_rect("white"),
        strip.text = element_text(hjust = 0, face = "bold"),
        panel.grid.major = element_blank(), panel.grid.minor = element_blank()) +
  guides(color=guide_legend(ncol=2, byrow = TRUE, reverse = TRUE),
         shape=guide_legend(ncol=2, byrow = TRUE, reverse = TRUE),
         linetype=guide_legend(ncol=2, byrow = TRUE, reverse = TRUE))  

ggsave("figureK1.pdf", height = 8, width = 8)
